Skip to content

Comments

swev-id: scikit-learn__scikit-learn-13439 implement Pipeline __len__#29

Open
casey-brooks wants to merge 1 commit intoscikit-learn__scikit-learn-13439from
casey/pipeline-len-13439
Open

swev-id: scikit-learn__scikit-learn-13439 implement Pipeline __len__#29
casey-brooks wants to merge 1 commit intoscikit-learn__scikit-learn-13439from
casey/pipeline-len-13439

Conversation

@casey-brooks
Copy link

Summary

  • addresses Pipeline should implement __len__ #28 by adding Pipeline.len so len(pipe) works with sequence-style helpers
  • adds regression tests for len() with passthrough/None final estimators and slicing via pipe[:len(pipe)]

Observed failure

After building scikit-learn in editable mode, reproducing the issue with the snippet from #28 fails:

python - <<'PY'
from sklearn import svm
from sklearn.datasets import samples_generator
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_regression
from sklearn.pipeline import Pipeline

X, y = samples_generator.make_classification(
    n_informative=5, n_redundant=0, random_state=42)

anova_filter = SelectKBest(f_regression, k=5)
clf = svm.SVC(kernel='linear')
pipe = Pipeline([('anova', anova_filter), ('svc', clf)])

print(len(pipe))
PY
Traceback (most recent call last):
  File "<stdin>", line 14, in <module>
TypeError: object of type 'Pipeline' has no len()

Testing

  • pytest sklearn/tests/test_pipeline.py -k len (2 passed)
  • flake8 sklearn/pipeline.py sklearn/tests/test_pipeline.py

@casey-brooks casey-brooks requested a review from a team December 26, 2025 01:53
@casey-brooks
Copy link
Author

Local validation:

  • pytest sklearn/tests/test_pipeline.py -k len (2 passed, 41 deselected)
  • flake8 sklearn/pipeline.py sklearn/tests/test_pipeline.py (no issues)

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great—thanks for the targeted implementation and thorough regression coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants